/*  basic function  */

export default function() {
  let o = {
    // 获取文档宽度
    getDocumentWidth() {
      return document.documentElement.clientWidth || document.body.clientWidth;
    },

    // 获取文档高度
    getDocumentHeight() {
      return (
        document.documentElement.clientHeight || document.body.clientHeight
      );
    },

    // localStorage封装
    setLocalStorage(key, data) {
      let storage = window.localStorage;
      let myData = data;
      if (typeof myData === "object" || typeof myData === "function") {
        storage.setItem(key, JSON.stringify(myData));
      } else {
        storage.setItem(key, myData);
      }
      return myData;
    },

    getLocalStorage(key) {
      let storage = window.localStorage;
      return storage.getItem(key);
    },

    // sessionStorage封装
    setSessionStorage(key, data) {
      let storage = window.sessionStorage;
      let myData = data;
      if (typeof myData === "object" || typeof myData === "function") {
        storage.setItem(key, JSON.stringify(myData));
      } else {
        storage.setItem(key, myData);
      }
      return myData;
    },

    getSessionStorage(key) {
      let storage = window.sessionStorage;
      return storage.getItem(key);
    },

    // 计算当前时间的时间差
    timeConvert(timeValue) {
      let date1 = timeValue; // 开始时间
      let date2 = new Date(); // 结束时间
      let date3 = date2.getTime() - new Date(date1).getTime(); // 时间差的毫秒数
      // 计算出相差天数
      let days = Math.floor(date3 / (24 * 3600 * 1000));
      // 计算出小时数
      let leave1 = date3 % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
      let hours = Math.floor(leave1 / (3600 * 1000));
      // 计算相差分钟数
      let leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
      let minutes = Math.floor(leave2 / (60 * 1000));
      // 计算相差秒数
      let leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
      let seconds = Math.round(leave3 / 1000);
      let showTime = "";
      if (days >= 0) {
        showTime = days + "天 " + hours + "小时" + minutes + "分";
      } else if (days < 0 || hours < 0 || minutes < 0 || seconds < 0) {
        showTime = "-";
      }
      return showTime;
    },

    // 个位数转化为'0x'形式
    singleAddZero(num) {
      return num < 10 ? "0" + num : num;
    },

    // date转化为string(yyyy-MM-dd hh-mm-ss)
    dateToString(newDate) {
      let tDate = newDate;
      if (!tDate) {
        tDate = new Date();
      }
      let yyyy = tDate.getFullYear();

      let MM = this.singleAddZero(tDate.getMonth() + 1);

      let dd = this.singleAddZero(tDate.getDate());

      let hh = this.singleAddZero(tDate.getHours());

      let mm = this.singleAddZero(tDate.getMinutes());

      let ss = this.singleAddZero(tDate.getSeconds());
      return yyyy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss;
    },

    // 生成随机字符串
    randomString(len) {
      len = len || 32;
      const $chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
      const maxPos = $chars.length;
      let pwd = "";
      for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return pwd;
    },

    /**
     * 随机整数
     * @param min 最小
     * @param max 最大
     * @param num 有此数就随机生成一个指定长度的数组
     * @returns {number}
     */
    mathRandom(min, max, num) {
      if (num) {
        let tempArr = [];
        for (let i = 0; i < num; i++) {
          tempArr.push(Math.floor(Math.random() * (max - min + 1) + min));
        }
        return tempArr;
      } else {
        return Math.floor(Math.random() * (max - min + 1) + min);
      }
    },
    // 全局formatter方法
    constFormatter(constant, val) {
      let res = "";
      constant.forEach(item => {
        if (item.value === val) {
          res = item.label;
        }
      });
      return res;
    },
    setCookie(name, value, time) {
      // let strsec = this.getsec(time)
      // let exp = new Date()
      // exp.setTime(exp.getTime() + strsec * 1)
      // document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
      let exdate = new Date();
      exdate.setDate(exdate.getDate() + time);
      document.cookie =
        name +
        "=" +
        escape(value) +
        (time == null ? "" : ";expires=" + exdate.toGMTString());
    },
    getCookie(name) {
      let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
      let arr = document.cookie.match(reg);
      if (arr) return arr[2];
      else return null;
    },
    delCookie(name) {
      let exp = new Date();
      exp.setTime(exp.getTime() - 1);
      let cval = this.getCookie(name);
      if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    },
    getsec(str) {
      let str1 = str.substring(1, str.length) * 1;
      let str2 = str.substring(0, 1);
      if (str2 === "s") {
        return str1 * 1000;
      } else if (str2 === "h") {
        return str1 * 60 * 60 * 1000;
      } else if (str2 === "d") {
        return str1 * 24 * 60 * 60 * 1000;
      }
    }
  };
  return o;
}
